package optional;

import optional.entity.Address;
import optional.entity.City;
import optional.entity.ITest;
import optional.entity.User;

import javax.swing.text.html.Option;
import java.util.Optional;

/**
 * @author brianxia
 * @version 1.0
 * @date 2021/11/24 0:25
 */
public class App {

    public static void main(String[] args) {

        User user = new User();
        if(user instanceof User){
            //判断user对象是否是User类的对象
            System.out.println("user是User类的对象");
        }else{
            System.out.println("user不是User类的对象");
        }

        if(user instanceof ITest){
            //判断user对象是否是User类的对象
            System.out.println("user是ITest的对象");
        }else{
            System.out.println("user不是ITest的对象");
        }

        user.setAddress(new Address());
        //user.setAddress(new Address(new City("abc")));

        //1.不判断是否为null
        //user.getAddress().getCity().getName();

        //2.判断为null
        if(user != null){
            Address address = user.getAddress();
            if(address != null){
                City city = address.getCity();
                if(city != null){
                    String name = city.getName();
                    System.out.println(name);
                }else{
                    System.out.println("走了这个分支");
                }
            }
        }

        //3.Optional判断
        //Optional<City> city = Optional.of(user.getAddress().getCity());
        Optional<String> s = Optional.ofNullable(user).map(User::getAddress).map(Address::getCity)
                .map(City::getName);

//        if(s.isPresent()){
//            System.out.println(s.get());
//        }
        String test = s.orElse("test");
        System.out.println(test);

    }
}
